﻿using System;
using System.Collections.Generic;
using System.Text;
using Lswweb.Model;
using Lswweb.Domain;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Lswweb.Controls
{
    public class UserLogin : SkinnedWebControl
    {
        #region 成员字段和构造方法

        private TextBox txtName;
        private TextBox txtPsw;
        private LanguageDropDownList ddlLanguage;
        private Control selectLanaguagePanel;
        private Button btnLogin;

        private string returnUrl = SiteContext.Current.ReturnUrl;

        #endregion

        #region 初始化控件

        protected override void InitializeSkin(Control skin)
        {
            txtName = (TextBox)skin.FindControl("txtName");
            txtPsw = (TextBox)skin.FindControl("txtPsw");
            ddlLanguage = (LanguageDropDownList)skin.FindControl("ddlLanguage");
            selectLanaguagePanel = skin.FindControl("selectLanaguagePanel");
            btnLogin = (Button)skin.FindControl("btnLogin");

            if (ResourceManagement.LanguageCollection.Count <= 1)
                selectLanaguagePanel.Visible = false;

            btnLogin.Click += new EventHandler(btnLogin_Click);
            ddlLanguage.AutoPostBack = false;
        }

        void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = txtName.Text.Trim();
            string userPassword = txtPsw.Text.Trim();

            if (selectLanaguagePanel.Visible)
                SiteContext.Current.Language = ddlLanguage.SelectedValue;
            
            UserSecurity.Login(userName, userPassword);
        }  

        #endregion

        protected override string DefaultSkinFilename
        {
            get { return "User/UserLogin.ascx"; }
        }
    }
}
